import logging
from utils.yaml_tool import read_data_from_yaml
from conf.settings import VAR_DATA_PATH

logger = logging.getLogger(__name__)

class CommonVar:
    def __init__(self):
        self._var_data = None  # 用于存储常用变量数据
        self._is_loaded = False  # 标记是否已经加载过常用变量

    def _load_var_data(self):
        """加载常用变量数据文件"""
        if not self._is_loaded:
            try:
                self._var_data = read_data_from_yaml(VAR_DATA_PATH)
                self._is_loaded = True
                logger.info("常用变量数据已加载")
            except Exception as e:
                logger.error(f"加载常用变量数据失败: {e}")
                self._var_data = {}
        else:
            logger.info("常用变量数据已缓存，直接使用")

    def getCommonVar(self):
        """获取常用变量数据"""
        self._load_var_data()
        return self._var_data

# 实例化并返回的单例对象
cv = CommonVar()